package com.randev.snapgallery;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageView;

@SuppressWarnings("deprecation")
public class ImageViewAdapter extends BaseAdapter {
	
	Context context;
	List<String> imageNameList = new ArrayList<String>();
	String folderName = "";
	String sdCard = Environment.getExternalStorageDirectory().getAbsolutePath();
	String HOME_DIRECTORY = sdCard + File.separator + "AndroidTest";
	
	public ImageViewAdapter(Context c, List<String> imgNameList, String fName) {
		context = c;
		imageNameList = imgNameList;
		folderName = fName;
	}

	@Override
	public int getCount() {
		return imageNameList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		ImageView iv = null;
		iv = new ImageView(context);

		iv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		iv.setPadding(10, 10, 10, 10);
		
		String imageId = imageNameList.get(arg0);
		try {
			File imageFile = new File(HOME_DIRECTORY + File.separator
					+ folderName + File.separator + imageId);

			if (imageFile.exists()) {

				try {
					DisplayMetrics metrics = context.getResources().getDisplayMetrics();
					int width = metrics.widthPixels;
					int height = metrics.heightPixels;
					iv.setImageBitmap(decodeSampledBitmapFromResource(
							String.valueOf(imageFile), width, height-400));

				} catch (IllegalArgumentException e) {
					Log.w("Illegal argument exception", e.toString());
				} catch (OutOfMemoryError e) {
					Log.w("Out of memory error :(", e.toString());
				}

			}
		} catch (Exception e) {
			Log.w("Error getting image file", e.toString());
		}
		return iv;
	}

	public static int calculateInSampleSize(BitmapFactory.Options options,
			int reqWidth, int reqHeight) {
		// Raw height and width of image
		final int height = options.outHeight;
		final int width = options.outWidth;
		int inSampleSize = 0;

		if (height > reqHeight || width > reqWidth) {
			if (width > height) {
				inSampleSize = Math.round((float) height / (float) reqHeight);
			} else {
				inSampleSize = Math.round((float) width / (float) reqWidth);
			}
		}
		return inSampleSize;
	}

	public static Bitmap decodeSampledBitmapFromResource(String path,
			int reqWidth, int reqHeight) {

		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(path, options);

		options.inSampleSize = calculateInSampleSize(options, reqWidth,
				reqHeight);

		options.inJustDecodeBounds = false;
		return BitmapFactory.decodeFile(path, options);
	}

}
